﻿using Itech_Run.HttpApi.Shared.Result;
using Itech_Run.Helper.Attribute;
using Itech_Run.HttpApi.Shared;
using Itech_Run.Sys.Application.Contracts.Dto;
using Itech_Run.Sys.Application.Contracts.Service;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Caching;

namespace Itech_Run.Sys.HttpApi.Controllers
{
    public class SysRoleController : BaseApiController
    {
        private readonly ISysRoleService _sysRoleService;
        private readonly IDistributedCache<Dictionary<string, List<ButtonAuthDto>>, string> _buttonCache;
        public SysRoleController(ISysRoleService sysRoleService, IDistributedCache<Dictionary<string, List<ButtonAuthDto>>, string> buttonCache)
        {
            _sysRoleService = sysRoleService;
            _buttonCache = buttonCache;
        }

        [ModuleOpen]
        [HttpGet]
        public async Task<CustomResult> GetListKeyValueAsync()
        {
            return new CustomResult(await _sysRoleService.GetListKeyValueAsync());
        }

        [HttpGet]
        public async Task<CustomResult> GetAsync(string id)
        {
            return new CustomResult(await _sysRoleService.GetAsync(id));
        }

        [HttpPost]
        public async Task<CustomResult> GetListAsync(SysRoleListDto listDto)
        {
            return new CustomResult(await _sysRoleService.GetListAsync(listDto));
        }

        private void RemoveCache()
        {
            _buttonCache.Remove(string.Concat("Button", "_", GetOperateId()));
        }

        [ButtonAuth(ButtonAuthConst.add)]
        [HttpPost]
        public async Task<CustomResult> InsertAsync(SysRoleCreateDto dto)
        {
            RemoveCache();
            string operateUser = GetOperateId();
            return new CustomResult(await _sysRoleService.InsertAsync(dto, operateUser));
        }

        [ButtonAuth(ButtonAuthConst.edit)]
        [HttpPost]
        public async Task<CustomResult> UpdateAsync(SysRoleUpdateDto dto)
        {
            RemoveCache();
            string operateUser = GetOperateId();
            return new CustomResult(await _sysRoleService.UpdateAsync(dto, operateUser));
        }

        [ButtonAuth(ButtonAuthConst.delete)]
        [HttpPost]
        public async Task<CustomResult> DeleteAsync(string id)
        {
            RemoveCache();
            await _sysRoleService.DeleteAsync(id);
            return GetSuccesResult();
        }
    }
}
